MyMineView = {}

local self = MyMineView
self.view = UIBase()
self.view.name = "MyMineView"
self.view:AddPath("ui/panels/mymine.gdmec")

self.view.onInit = function()
    self:OnInit()
end

self.view.onUnit = function()
    self:OnUnit()
end

function self:Open()
    self.view:Open()
end

function self:Close()
    self.view:Close()
end

function self:OnInit()
    self.cells = {}
    self.ui_btn_close = self.view.go.transform:Find("BtnClose"):GetComponent(typeof(UnityEngine.UI.Button))
    self.ui_btn_close.onClick:AddListener(function()
        self:Close()
    end
)
    -- self.ui_btn_diff = self.view.go.transform:Find("ButtonDiff"):GetComponent(typeof(UnityEngine.UI.Button))
    -- self.ui_btn_diff.onClick:AddListener(
    --     function()
    --         MyMineControl:SetDiff(MyMineConstant.DiffLev.I)
    --     end
    -- )

    --[[
        1.我们self.view.go == MyMineView.prefab    
        2. self.view.go 找 BtnClose 对象
        3. 找到 BtnClose 对象之后 我希望 找到他自己身上属性 c#Button  lua:UnityEngine.UI.Button
        4. 找到了  UnityEngine.UI.Button 组件 我希望添加一个点击事件 
        5. 添加一个匿名函数 -> 调用 当前 对象的关闭函数self:Close()
    --]]
    -- local BtnClose = self.view.go.transform:Find("BtnClose")
    -- local Button = BtnClose:GetComponent(typeof(UnityEngine.UI.Button))
    -- Button.onClick:AddListener(function()
    --     self:Close()
    -- end)

    local btn_start = self.view.go.transform:Find("BtnStart"):GetComponent(typeof(UnityEngine.UI.Button))
    btn_start.onClick:AddListener(function()
        MyMineControl:OnStart()
    end
)

    self.ui_layout_root = self.view.go.transform:Find("Layout")
    self.ui_cell_prefab = self.view.go.transform:Find("Layout/CellPrefab")
    self.ui_cell_prefab.gameObject:SetActive(false)
    self.func_all_up=function()
        self:UpView()

        end

        EventListener:Listener(MyMineEvent.MINE_ALL_UP,self.func_all_up)
        self:UpView()
        end

function self:UpView()

    local idx = 0
    local cell=nil
    local cell_data=nil
    for y = 1, 14 do
        for x = 1, 9 do
            idx = x + (y - 1) * 9
            cell=self:MakeCell(idx, x, y) 
            if cell then
                 cell_data = MyMineModel:GetCell(x,y)
                if cell_data then
                cell.act.gameObject:SetActive(cell_data.is_dig)
                cell.num.text=cell_data.bomb_num==0 and "" or tostring(cell_data.bomb_num)
                if MyMineModel.game_status==MyMineConstant.GameStatus.GameOver then
                    if cell_data.is_bomb then
                        cell.num.text=""
                        cell.bomb.gameObject:SetActive(true)
                    end
                    cell.act.gameObject:SetActive(true)
                 end
                end
            end 
        end
    end
end

function self:MakeCell(idx, x, y)
    local cell = self.cells[idx]
    if cell == nil then
        local clone = UnityEngine.GameObject.Instantiate(self.ui_cell_prefab, self.ui_layout_root)

        cell = {
            clone = clone,
            idx = idx,
            x = x,
            y = y,
            act = clone.transform:Find("Act"),
            bomb = clone.transform:Find("Bomb"),
            num = clone.transform:Find("Act/Num"):GetComponent(typeof(UnityEngine.UI.Text)),
            val = clone.transform:Find("Val"):GetComponent(typeof(UnityEngine.UI.Text))
        }
        local btn = clone:GetComponent(typeof(UnityEngine.UI.Button))
        btn.onClick:AddListener(function()
            self:OnDig(idx, x, y)
        end)
        self.cells[idx] = cell
    end
    cell.act.gameObject:SetActive(false)
    cell.bomb.gameObject:SetActive(false)
    cell.num.text = ""
    cell.val.text = "idx:" .. idx .. "x" .. x .. "y" .. y
    cell.clone.gameObject:SetActive(true)
    return cell
end

function self:OnDig(idx, x, y)
    MyMineControl:OnDig(idx, x, y)
    Debug.Log("idx" .. idx .. "x" .. x .. "y" .. y)
end
function self:OnUnit()

end
